#include<iostream>
#include<map>
using namespace std;
typedef long long LL;
map<string, LL>mp;
string s;
int n, m, z, w;
int main()
{
	cin >> n >> m;
	for (int i = 0; i < n; i++) {
		cin >> z;
		for (int i = 0; i < z; i++) {
			cin >> s;
			cin >> w;
			mp[s] += w;
		}
	}
	LL ans = 0;
	for (int i = 0; i < m; i++) {
		cin >> s >> w;
		ans += mp[s] * w;
	}
	cout << ans << endl;
	return 0;
}